#!/usr/bin/env python
# -*- coding: utf-8 -*-

import wx
from core import Core
from core.ide.commandmanager import CommandManager
from core.utils.cmdenum import UPDATE_SETTINGS
from core.ide.langsmanager import LangsManager
import wx.propgrid as wxpg
from core.ide.datamangager import DataMangager,DataInfos,DataObject

class PropertyPanel(wx.Frame):
    def __init__(self,project):
        style=wx.DEFAULT_FRAME_STYLE | wx.SUNKEN_BORDER | wx.CLIP_CHILDREN
        wx.Frame.__init__(self,Core().getMainFrame(),-1, "Project Properties", size=(400,420),style=style)
        self.panel=wx.Panel(self)
        
        self.project=project
        
        self.nb=NotebookProperty(self.panel)
        self.createPage()
        
        btnbox=wx.BoxSizer(wx.HORIZONTAL)
        btnbox.Add((10,10), 1, wx.EXPAND)
        self.ok=wx.Button(self.panel,-1,"OK")
        btnbox.AddSpacer(5)
        btnbox.Add(self.ok, 0, wx.EXPAND)
        self.cancel=wx.Button(self.panel,-1,"Cancel")
        btnbox.AddSpacer(5)
        btnbox.Add(self.cancel, 0, wx.EXPAND)
        self.apply=wx.Button(self.panel,-1,"Apply")
        btnbox.AddSpacer(5)
        btnbox.Add(self.apply, 0, wx.EXPAND)
        btnbox.AddSpacer(12)
        
        hbox = wx.BoxSizer(wx.HORIZONTAL)
        hbox.AddSpacer(10)
        hbox.Add(self.nb, 1, wx.EXPAND)
        hbox.AddSpacer(10)
    
        vbox = wx.BoxSizer(wx.VERTICAL)
        vbox.AddSpacer(10)
        vbox.Add(hbox, 1, wx.EXPAND)
        vbox.AddSpacer(8)
        vbox.Add(btnbox, 0, wx.EXPAND)
        vbox.AddSpacer(12)
        self.panel.SetSizer(vbox)
        
        
        self.Bind(wx.EVT_BUTTON,self.onApply,self.apply)
        self.Bind(wx.EVT_BUTTON,self.onCancel,self.cancel)
        self.Bind(wx.EVT_BUTTON,self.onOK,self.ok)
    
    
    def onCancel(self,e=None):
        self.Destroy()
    
    def onOK(self,e=None):
        self.onApply()
        self.Destroy()
    
    def onApply(self,e=None):
        self.project.save()
        CommandManager().execute(UPDATE_SETTINGS)
    
    def createPage(self):
        self.nb.AddPage(wx.Panel(self.nb), "General") #Options: add custom setting (duplicate form global setting); add source path
        
        self.lang=LangsManager().getLangFromFile(self.project.getDocumentRoot())
        self.nb.addSettingPage(self.lang.name,self.project.getDataObject())
        #self.data
        self.nb.AddPage(wx.Panel(self.nb), "Infos")
    
    
class NotebookProperty(wx.Notebook):
    def __init__(self,parent):
        wx.Notebook.__init__(self,parent)
        
    def addSettingPage(self,name,data):
        panel=wx.Panel(self)
        self.AddPage(panel, name)
        grid=PropertyGrid(panel,(200,200),data,name)
        box=wx.BoxSizer(wx.HORIZONTAL)
        box.Add(grid, 1, wx.EXPAND)
        panel.SetSizer(box)
        
        
        
        
class PropertyGrid(wxpg.PropertyGrid):
    def __init__(self, parent, size, dataObject, section=None):
        wxpg.PropertyGrid.__init__(self,parent,size=size,
                        style=wxpg.PG_SPLITTER_AUTO_CENTER |
                              wxpg.PG_AUTO_SORT |
                              wxpg.PG_TOOLBAR #|
                              #wx.BORDER_NONE
                              )
        self.dataObject=dataObject
        self.section=section
        if not self.section:
            self.section=self.dataObject.getSections()[0]
        
        self.Bind( wxpg.EVT_PG_CHANGED, self.onPropGridChange )
        
        self.SetExtraStyle(wxpg.PG_EX_HELP_AS_TOOLTIPS)
        
        #self.SetCaptionTextColour(wx.Colour(50,80,20))
        self.SetCaptionTextColour(wx.Colour(0,0,0))
        self.SetCaptionBackgroundColour(wx.Colour(229,237,222))
        self.SetMarginColour(wx.Colour(229,237,222))
        
        self.populate()
    
    def onPropGridChange(self,e=None):
        p = e.GetProperty()
        p.GetCell(0).SetFont(wx.Font( 8, wx.FONTFAMILY_SWISS, wx.FONTSTYLE_NORMAL, wx.FONTWEIGHT_BOLD ))
        if self.dataObject.hasValue(self.section,p.GetName()):
            self.dataObject.setValue(self.section,p.GetName(),p.GetValue())
        else:
            data=DataMangager().getData(self.section,p.GetName())
            self.dataObject.addDataInfos(DataInfos(self.section,p.GetName(),p.GetValue(),data.type,data.options,data.category,data.help))
        
    def populate(self):
        datas=DataMangager().getDatas(self.section)
        datas2=None
        if self.dataObject.hasSections(self.section):
            datas2=self.dataObject.getDatas(self.section)
        
        categories={}
        for dataName in datas:
            if datas2 and datas2.has_key(dataName):
               data=datas2[dataName]
               data.temp=True
            else:  
                data=datas[dataName]
            if not categories.has_key(data.category):
                categories[data.category]=[]
            categories[data.category].append(data)
            
        for category in categories:
            datas=categories[category]
            self.Append(wxpg.PropertyCategory(category))
            for data in datas:
                self.createProperty(data,data.temp)
        
    def createProperty(self,data,bold=False):
        #data=DataInfos()
        if data.type==data.STRING:
            pgproperty = self.Append( wxpg.StringProperty(data.name,value=data.value))
        elif data.type==data.INT:
            pgproperty = self.Append( wxpg.IntProperty(data.name,value=data.value))
        elif data.type==data.FLOAT:
            pgproperty = self.Append( wxpg.FloatProperty(data.name,value=data.value))
        elif data.type==data.BOOL:
            pgproperty = self.Append( wxpg.BoolProperty(data.name,value=data.value))
        elif data.type==data.ENUM:
            pgproperty = self.Append( wxpg.EnumProperty(data.name,labels=data.options,value=data.value,values=range(len(data.options))))
        elif data.type==data.FILE:
            pgproperty = self.Append( wxpg.FileProperty(data.name,value=data.value))
        elif data.type==data.DIR:
            pgproperty = self.Append( wxpg.DirProperty(data.name,value=data.value))
        #font=pgproperty.GetCell(0).SetFont()
        if bold:
            pgproperty.GetCell(0).SetFont(wx.Font( 8, wx.FONTFAMILY_SWISS, wx.FONTSTYLE_NORMAL, wx.FONTWEIGHT_BOLD ))
    
    
    def setSection(self,name):
        self.Clear()
        self.section=name
        self.populate()

        
        
